<template>
	<div style="padding:10px">
		<Form ref="loginForm" :model="form" :rules="rules">
			<FormItem label="公文状态" prop="value">
				<RadioGroup v-model="form.value">
					<Radio label="yes">通过</Radio>
					<Radio label="no">不通过</Radio>
					<!-- <Radio label="no_sure">通过并向上级请示</Radio> -->
				</RadioGroup>
			</FormItem>
			<div style="margin-bottom:23px" v-if="form.value=='yes'">
				<p style="margin-bottom:10px">批示：</p>
				<Input v-model="form.approval.zh" type="textarea" :rows="4" placeholder="输入批示..."></Input>
			
			</div>
			<div style="margin-bottom:23px" v-else-if="form.value=='no'">
				<p style="margin-bottom:10px">意见：</p>
				<Input v-model="form.content.zh" type="textarea" :rows="4" placeholder="输入意见..."></Input>
				
			</div>
			<div v-if="form.value=='no_sure' || form.value=='yes'">
			<FormItem :label="form.value=='no_sure'?'选择审核人':'选择查阅人'" :prop="form.value=='no_sure'?'examine_user':'teacher'">
				<Input v-if="form.value=='no_sure'" v-model="exname" @on-focus="modal=true" placeholder="请选择审核人" style="width: 200px"></Input>
				<Input v-else-if="form.value=='yes'" v-model="exnameteacher" @on-focus="modalteacher=true" placeholder="选择查阅人" type="textarea" :autosize="{minRows: 2,maxRows: 5}"></Input>
			</FormItem>
			</div>
			<Button type="primary" @click="moralAdd" long :loading="loading">提交</Button>
		</Form>
		<Modal v-model="modal" title="选择审核人" @on-cancel="form.examine_user=''" width="350">
			<auditor @on-select="(id)=> {this.form.examine_user=id}"></auditor>
		</Modal>

		<Modal v-model="modalteacher" title="选择查阅人" @on-cancel="cancelstatus" width="350">
			 <div style="text-align:center;margin-bottom:10px">
				<RadioGroup v-model="partype" type="button" size="small" @on-change="ptchange">
					<Radio label="dus">职务</Radio>
					<Radio label="dep">所有人</Radio>
				</RadioGroup>
			</div>
			<Input v-model="seartxt" size="small" placeholder="搜索人员"  @on-change="toSearch(seartxt)" v-if="partype=='dep'" style="width: 300px;"/>
			<Select v-model="dutie_id" v-if="partype=='dus'" size="small" @on-change="chax" placeholder="请选择职务">
				<Option :value="item.value" v-for="(item,ind) in duties" :key="ind">{{item.title}}</Option>
			</Select>
			<div style="min-height:250px;margin-top:10px;height:400px;overflow:auto">
				<CheckboxGroup v-model="form.teacher" @on-change="toselec">
					<div v-for="item in wonima" :key="item.user_id" style="margin-bottom:2px"><Checkbox :label="item.user_id">
						{{item.teacher_name.zh||item.teacher_name.mn}}</Checkbox></div>
				</CheckboxGroup>
			</div>
		</Modal>
	</div>
</template>

<script>

export default {
	
	components: {
		Auditor: re=> require(['&/Public/SelectPeople/auditor.vue'],re)
	},

    props:{
		id:Number,
		userId:Number,
	},

    data () {
        return {
			getypeurl:"api/dictionary/v1/dictionary/pidquerys?document_type",
			auditUrl:'api/document/v1/document/access/',
            form:{
				value:'',
				approval:{
					zh:'',
					en:'',
					mn:'',
				},
				content:{
					zh:'',
					en:'',
					mn:'',
				},
				examine_user:'',
				teacher:[],
			},
			partype: 'dus',
            dutie_id: '',
            seartxt: '',
            duties: [],

			loading: false,
			modal: false,
			modalteacher:false,
			rules: {
				value: [
                    { required: true, message: '请审核', trigger: 'change' }
				],
				'examine_user': [
					{ required: true, message: '审核人不能为空', trigger: 'change', type: 'number'}
				],
				'teacher': [
					{ required: true,type:'array', message: '至少选择一个传阅人', trigger: 'change', }
				],
			},
			examiners: [],
			examiners_copy: [],
			wonima:[],
			// 上传路径
			upload_url: this.$api+'/api/user/v1/editor/upload?module=documents',
            upload_token: localStorage.token,
        }
    },
    methods: {
		toselec(tp) {
            this.$emit('on-select',tp)
        },
		ptchange(type) {
            if(type=='dep') {
				this.wonima = this.examiners_copy
			} else {
				this.wonima = []
				this.dutie_id = ''
			}
        },
		chax(id) {
            let url = `api/user/v1/get/teacher/user/list?duties_id=[${id}]`
			this.$http.get(url).then(res=>{
				this.wonima = res.data
			}).catch(
				err => this.$Message.error('人员获取失败！')
			)
        },
		// 搜索人员
		toSearch(txt) {
			if(txt.length!=0) {
				this.wonima = this.examiners_copy.filter(item=> {
					return new RegExp(txt).test(item.teacher_name)
				})
			} else {
				this.wonima = this.examiners_copy
			}
		},
		zwload() {
			this.$http.get('api/user/v1/get/duties/list').then(res=> {
				this.duties = res.data.map(item=> ({
                    value: item.id,
                    title: item.duties_name.zh?item.duties_name.zh:item.duties_name.mn
                }))
			}).catch(
				err => this.$Message.error('职务加载失败！')
			)
        },
		cancelstatus(){
			this.form.examine_user = ''
			this.form.teacher = []
		},
		load(){
            this.$http.get('api/document/v1/document/getone_examine/'+this.id)
            .then(res=>{
                if(res.code==200){
                    this.form.content=res.data.content
                    if(res.data.status==0){
                        this.form.value = ''
                    }else if(res.data.status==2){
                        this.form.value = 'no'
                    }
                }
            },err=>console.log(err))
		},
		panHide() {
            this.$emit('close')
            this.$emit('reload')
		},
		// 点击添加按钮将数据发送给后端
		moralAdd() {
			if(this.form.value=='yes'){
				delete this.form.examine_user
			}else if(this.form.value=='no'){
				delete this.form.examine_user
				delete this.form.teacher
			}else{
				delete this.form.teacher
			}
			this.$refs.loginForm.validate((valid) => {
				if (valid) {
					this.loading = true
					this.$http.put(this.auditUrl+this.id,this.form).then(val=> {
						if(val.code==200) {
							this.panHide();
							this.loading = false
						} else {
							this.loading = false
						}
					})
				} else {
					this.$Message.error('验证失败！');
				}
			})
		},
		getExamine(){
			this.$http.get('api/user/v1/get/teacher/user/list').then(val=>{
				// val.data.splice(val.data.findIndex(item=>item.user_id==this.userId),1)
				this.examiners = val.data
				this.wonima = []
				this.examiners_copy = val.data
			}).catch(
				err => console.log(err)
			)
		},
    },
	computed: {
		exname() {
			let bool = this.examiners.find(item=>this.form.examine_user==item.user_id)
			if(bool) {
				return bool.teacher_name?bool.teacher_name.zh?bool.teacher_name:'':''
			} else {
				return ''
			}
		},
		exnameteacher(){
			if(this.form.teacher.length>0){
				let arrar= []
				this.form.teacher.forEach(el=>{
				this.examiners.forEach(item=>{
					if(el==item.user_id){
						arrar.push(item.teacher_name.zh||item.teacher_name.mn)
					}
				})
			})
			return arrar.join(',')
			}
		},
		
	},
    created(){
		this.getExamine()
		this.load()
		this.zwload()
	},
	watch:{
	}
}
</script>

<style scoped>

</style>